﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zichan.DAL;
using zichan.Entity;

namespace CSVOS
{
   static class CsvFileReader
    {
        public static List<dataModel> Read(string fullname)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                List<int> ids = new List<int> { 7, 8, 11 };
                List<string> ips = db.IPs.Where(o => ids.Contains(o.MachineRoomId)).Select(o => o.Ip).ToList();
                List<dataModel> dataList = new List<dataModel>();
                if (!File.Exists(fullname)) return dataList;
                var lines = File.ReadAllLines(fullname, System.Text.Encoding.Default).Skip(2);
                var list = new List<string[]>();
                var builder = new StringBuilder();
                foreach (var line in lines)
                {
                    string sline=line.Replace("\"", "");
                    //Byte[] encodedBytes1 = Encoding.UTF8.GetBytes(line);
                    //string l = Encoding.GetEncoding("GBK").GetString(encodedBytes1);
                    string[] str = sline.Split(new char[] { ',','，' });
                    dataModel model = new dataModel();
                    model.adress = str[0];
                    model.No = str[1];
                    model.WanAn = str[2];
                    model.FuWuShan = str[3];
                    model.WebName = str[4];
                    model.ZhuYuMing = str[5];
                    model.Bip = str[6];
                    model.Eip = str[7];
                    model.DW1 = str[8];
                    model.DW2 = str[9];
                    model.Name = str[10];
                    model.Number = str[11];
                    model.Phone = str[12];
                    model.Email = str[13];
                    model.YY = str[14];
                    model.YUMing =str.Length>15? str[15]:"";
                    model.Pip = str.Length > 16 ? str[16] : "";
                    model.Remark = str.Length > 17 ? str[17] : "";
                    string ip = model.Pip;
                    if(model.Pip.Contains("/"))
                    {
                        ip = model.Pip.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
                    }
                    if (ips.Contains(ip))
                        model.TrueOrNo = "是";
                    else
                        model.TrueOrNo = "否";
                    dataList.Add(model);

                }
                return dataList;
            }
        }
    }
}
